package com.longge;

import java.util.Arrays;

public class DivideArrayII {

    public int[][] divideArray(int[] nums, int k) {

        Arrays.sort(nums);

        int n = nums.length;
        int[][] ans = new int[n / 3][3];
        int i = 0, j = 0, index = 0;
        while (i < n) {

            while (j < 3) {
                ans[index][j] = nums[i];
                if (j==2){
                   if (ans[index][j]-ans[index][j-1]>k){
                       return new int[][]{};
                   }
                }
                j++;
                i++;
            }
            j = 0;
            index++;
        }
        return ans;
    }
}
